<?php
namespace Home\Controller;

use Utuo\ForegroundController;

class CartTeamController extends ForegroundController {
	
	public function _init() {
		layout(false);
		$this->setModelName("Site");
	}
	
	/**
	 * 详情
	 */
	public function showAction() {
		function hide( $str, $s, $e) {
			if(mb_strlen($str, 'utf8')>$s+$e) {
				$startStr = mb_substr($str, 0, $s, 'utf8');
				$endStr = mb_substr($str, mb_strlen($str, 'utf8')-$e, $e, 'utf8');
				$middleStr = str_repeat("*", mb_strlen($str, 'utf8') - mb_strlen($startStr, 'utf8') - mb_strlen($endStr, 'utf8'));
				$str = $startStr.$middleStr.$endStr;
			}
			return $str;
		}
		$id = I("id", 0, "intval"); // 审团 id
		/*状态*/
		$cartTeam = M("CartTeam")->where('id=%d', $id)->find();
		$cartTeam['progStatus'] = 1; // 进行中
		if(time() > $cartTeam['end_time']) {
			$cartTeam['progStatus'] = 0; // 已结束
		}
		/*所有参与*/
		if($cartTeam['progStatus']===0) {
			$userLists = M("CartTeamUser")->where("ac_id=%d and verify=1", $id)->select();
			foreach ($userLists as &$v) {
				$v['tel'] = hide($v['tel'], 2, 3);
			}
			$cartTeam['userLists'] = $userLists;
		}
		$this->assign("cartTeam", $cartTeam);
		$this->display("Site/teamShow");
	}
	
	public function indexAction() {
		
	}
	
	/**
	 * 表单提交
	 */
	public function saveAction() {
		C("TOKEN_ON", true);
		$id = I("ct_id", 0, "intval"); // 审团 id
		$m = M("CartTeamUser");
		$cartTeam = M("CartTeam")->where('id=%d', $id)->find();
		$cartTeam or $this->ajaxError("车审团不存在");
		(time() > $cartTeam['end_time']) and  $this->ajaxError("抱歉车审团已结束.");
		if($m->create()) {
			$m->create_time = time();
			$m->ac_id = $id;
			false !== $m->add() and $this->ajaxSuccess('报名成功，我们会尽快与您联系。');
		}
		$this->ajaxError('提交失败');
	}
	
	/**
	 * 车审团列表
	 */
	public function listAction() {
		$m = M("CartTeam");
		$cartTeamLists = $m->where(array('cid'=>$this->cid, 'status'=>1, 'is_deleted'=>0))->limit(10)->order("id desc")->select();
		foreach ($cartTeamLists as &$v) {
			$v['progStatus'] = 1; // 进行中
			if(time() > $v['end_time']) {
				$v['progStatus'] = 0; // 已结束
			}
			$v['url'] = U("CartTeam/show",array('id'=>$v['id'], 'sid'=> $this->cid));
		}
		$this->assign("cartTeamLists", $cartTeamLists);
		$this->display("Site/cartTeamList");
	}
	
}